var vm = new Vue({
    el: '#contentContainer',
    data: {
        tableData: [],
        pageNum: 1,
        pageSize: 20,
        pageSizes: [20, 30, 40, 50],
        total: 0,
        tableHeight: 400,
        dialogFormVisible: false,
        selectedRow: null,
        form: {
            withdrawReason: null,
        },
        rules: {
            withdrawReason: [
                {required: true, message: '请输入撤回原因', trigger: 'blur'},
                {min: 0, max: 500, message: '长度不能超过500个字符', trigger: 'blur'}
            ]
        }
    },
    created: function () {
        this.loadData();
    },
    mounted: function () {
        //动态计算table的高度
        var winHeight = $(window).height();
        var paginationHeight = $('#pagination').height() || 0;
        var tableMargin = parseInt($('.dataTable').css('margin-top') || 0)
            + parseInt($('.dataTable').css('margin-bottom') || 0);
        var containerPadding = parseInt($('.contentContainer').css('padding-top') || 0)
            + parseInt($('.contentContainer').css('padding-bottom') || 0);

        this.tableHeight = winHeight - paginationHeight
            - tableMargin - containerPadding;
    },
    methods: {
        loadData: function () {
            var $this = this;

            Common.getJson('/cms/content/getContentAuditList',
                {
                    pageNum: $this.pageNum, pageSize: $this.pageSize
                },
                function (response) {
                    if (!response || response.status != 200)
                        return;

                    var result = response.result;
                    $this.tableData = result.list;
                    $this.pageNum = result.pageNum;
                    $this.pageSize = result.pageSize;
                    $this.total = result.total;
                });
        },
        onSizeChange: function (size) {
            this.pageNum = 1;
            this.pageSize = size;
            this.loadData();
        },
        onCurrentChange: function (currentPage) {
            if (this.pageNum === currentPage)
                return;

            this.pageNum = currentPage;
            this.loadData();
        },
        onAudit: function (index, row) {
            this.selectedRow = row;

            var $this = this;
            this.$confirm('是否确认审核通过?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                callback: function (action, instance) {
                    if (action == 'cancel')
                        return;

                    if (row.contentStatus != 1) {
                        $this.$message({
                            type: 'error',
                            message: "只能审核状态为【审核中】的内容"
                        });

                        return;
                    }

                    Common.postJson('/cms/content/audit', {businessKey: row.contentId},
                        function (response) {
                            if (!response)
                                return;

                            //审核成功
                            if (response.status == 200 && response.result.success) {
                                $this.$message({
                                    type: 'success',
                                    message: '审核成功'
                                });

                                $this.pageNum = 1;
                                $this.loadData();
                            } else {
                                var errMsg = response.status == 200 ? response.result.errMsg : response.msg;

                                $this.$message({
                                    type: 'error',
                                    message: errMsg
                                });
                            }
                        });
                }
            });
        },
        onWithdraw: function (index, row) {
            this.selectedRow = row;

            if (row.contentStatus != 1) {
                this.$message({
                    type: 'error',
                    message: "只能撤回状态为【审核中】的内容"
                });

                return;
            }

            this.dialogFormVisible = true;
        },
        preview: function (contentId) {
            window.location.href = '/cms/content/preview/' + contentId;
        },
        submitForm: function (formName) {
            var $this = this;

            $this.$refs[formName].validate(function (valid) {
                if (!valid)
                    return false;

                $this.dialogFormVisible = false;

                Common.postJson('/cms/content/withdraw',
                    {
                        businessKey: $this.selectedRow.contentId,
                        comment: $this.form.withdrawReason
                    },
                    function (response) {
                        $this.form.withdrawReason = null;

                        if (!response)
                            return;

                        //撤回成功
                        if (response.status == 200 && response.result.success) {
                            $this.$message({
                                type: 'success',
                                message: '撤回成功'
                            });

                            $this.pageNum = 1;
                            $this.loadData();
                        } else {
                            var errMsg = response.status == 200 ? response.result.errMsg : response.msg;

                            $this.$message({
                                type: 'error',
                                message: errMsg
                            });
                        }
                    });
            });
        },
        showAuditSteps: function (contentId) {
            window.location.href = '/process/definition/processInstanceSteps?businessKey=' + contentId;
        }
    }
});